package com.cloudprovidepayment.controller;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.api.commons.pojo.CommonResult;
import com.cloud.api.commons.pojo.Payment;
import com.cloudprovidepayment.service.PaymentService;

import lombok.extern.slf4j.Slf4j;

@SuppressWarnings(value={"rawtypes", "unchecked"})
@RestController
@Slf4j
public class PaymentController {
	
	@Value(value = "${server.port}")
	private String serverPort;
	
	//注入服务发现的注解
	@Autowired
	private DiscoveryClient discoveryClient;
	
	@Autowired
	private PaymentService paymentService;

	//获取服务信息
	@GetMapping("/payment/discovery")
	public Object discovery() {
	   List<String> services = discoveryClient.getServices();
        for (String strItem : services) {
            log.info("********注册到eureka中的服务中有:" + strItem);
        }
	    List <ServiceInstance> instances = discoveryClient.getInstances("MCROSERVICE-PAYMENT");
        for (ServiceInstance s : instances) {
            log.info("当前服务的实例有"+s.getServiceId()+"\t"+s.getHost()+"\t"+s.getPort()+"\t"+s.getUri());
        }
	    return this.discoveryClient;
	}
	
	/**
	 * 插入数据
	 * @param dept
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/payment/create")
	public CommonResult create(@RequestBody Payment dept) throws Exception {
		dept.setCreateUser("临时插入");
		dept.setCreateUserID(001);
		int i = paymentService.create(dept);
		log.info("***************插入成功*******" + dept.getId());
		if (i == 0) {
			return new CommonResult(444, "插入数据库失败");
		}
		return new CommonResult(200, "插入数据库成功" + serverPort, dept.getId());
	}

	/**
	 * 根据id查询数据
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/payment/get/{id}")
	public CommonResult queryById(@PathVariable("id") String id) throws Exception {
		Payment payment = new Payment();
		payment.setId(id);
		payment = paymentService.queryById(payment);
		log.info("***************查询成功*********" + payment);
		if (payment == null) {
			return new CommonResult(444, "查询失败");
		}
		return new CommonResult(200, "查询成功" + serverPort, payment);
	}
	
	/**
	 * 模拟业务接口延时2秒
	 * @return
	 * @throws InterruptedException
	 */
	@GetMapping("/payment/feign/timeout")
	public String PaymentFeignTimeOut() throws InterruptedException {
	     TimeUnit.SECONDS.sleep(2);
	     return serverPort;
	}
	
	/**
	 * 获取serverPort
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/payment/lb")
	public String lb() throws Exception {
		return serverPort;
	}

}